<template>
  <div>
    <v-tableTitle
      :btnConfig="btnConfig"
      :tableSelectData="tableSelectData"
      title="快递接口配置"
      @del="del"
      @newCreate="newCreate"
    ></v-tableTitle>
    <GTable
      ref="table"
      :hasCheck="true"
      :hasEdit="true"
      :api="apiMap.getListApi"
      :columnSource="baseColumns"
      :cbFun="cbFun"
      @selection-change="(data) => (tableSelectData = data)"
      @setdialog="setdialog"
    />

    <el-dialog
      :title="dialogModule.title"
      :visible.sync="dialogModule.flag"
      center
      v-dialogDrag
      width="720px"
      @closed="closed"
    >
      <div v-tit="'快递接口配置'">
        <GForm
          ref="detail"
          :config="formConfig"
          :rules="formRules"
          labelWidth="120px"
          v-model="form"
        ></GForm>
      </div>
      <span slot="footer">
        <el-button type="primary" @click="handleConfirm">确定</el-button>
        <el-button @click="dialogModule.flag = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import tableTitle from "commons/TableTitle";

export default {
  name: "apiBase",
  data() {
    return {
      tableSelectData: [],
      dialogModule: {
        loading: false,
        flag: false,
        title: "",
        data: null,
        type: "",
      },
      form: {
        applicationId: null,
        structureCode: "",
        apiRequestRootType: "",
        apiRequestTemplate: "",
        apiResponseTemplate: "",
        remark: "",
        logkey: "",
      },
      options: {
        app: [],
        company: [],
        type: [],
        expressapi:[]
      },
    };
  },
  computed: {
    btnConfig() {
      return [
        {
          label: this.$t("common.create"),
          invalid: "newCreate",
        },
        {
          label: this.$t("common.delete"),
          disabled: true,
          invalid: "del",
        },
      ];
    },
    baseColumns() {
      return [
        {
          label: "配置名称",
          prop: "configname",
          input: "input",
        },
        {
          label: "生效仓库",
          prop: "wmwhseid",
          input: "input",
        },
        {
          label: "出库单货主编码",
          prop: "storerkey",
          input: "input",
        },
        {
          label: "出库单承运商编码",
          prop: "carriercode",
          input: "input",
        },
        {
          label: "出库单平台编码",
          prop: "rtxsalesp",
          input: "input",
        },
        {
          label: "快递接口",
          prop: "expressapi",
          input: 'select',
          options: this.options.expressapi
        },
        {
          label: "打印模板url",
          prop: "templatesurl",
          input: "input",
          desc: "货主平台 为淘系 拼多多 抖音需填",
        },
        {
          label: "平台承运商编码",
          prop: "platformCarriercode",
          input: "input",
          desc: "",
        },
        {
          label: "刷新token",
          prop: "refreshtoken",
          input: "input",
        },
        {
          label: "发货人名称",
          prop: "sendName",
          input: "input",
        },
        {
          label: "发货公司",
          prop: "sendCompanyName",
          input: "input",
        },
        {
          label: "发货人手机",
          prop: "sendMobile",
          input: "input",
        },
        {
          label: "发货人电话",
          prop: "sendPhone",
          input: "input",
        },
        {
          label: "发货人省份",
          prop: "sendProvince",
          input: "input",
        },
        {
          label: "发货人省份",
          prop: "sendProvince",
          input: "input",
        },
        {
          label: "发货人城市",
          prop: "sendCity",
          input: "input",
        },
        {
          label: "发货人区县",
          prop: "sendCounty",
          input: "input",
        },
        {
          label: "发货人乡镇街道",
          prop: "sendTown",
          input: "input",
        },
        {
          label: "发货人详细地址",
          prop: "sendAddress",
          input: "input",
        },
        {
          label: "接口请求url",
          prop: "url",
          input: "input",
        },
        {
          label: "appkey",
          prop: "appkey",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "appSecret",
          prop: "appSecret",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "clientId 拼多多",
          prop: "clientId",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "clientSecret 拼多多",
          prop: "clientSecret",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "sign 德邦",
          prop: "sign",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "companycode 德邦",
          prop: "companycode",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "ztoCompanyid 中通",
          prop: "ztoCompanyid",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "ztoKey 中通",
          prop: "ztoKey",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "ztoPartner 中通",
          prop: "ztoPartner",
          input: "input",
          desc: "可配置",
        },
        {
          label: "stoSecretkey 申通",
          prop: "stoSecretkey",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "stoSiteCode 申通",
          prop: "stoSiteCode",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "stoCustomerName 申通",
          prop: "stoCustomerName",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "stoCode 申通",
          prop: "stoCode",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "sitePwd 申通",
          prop: "sitePwd",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "sfCustid 顺丰",
          prop: "sfCustid",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "sfCheckword 顺丰",
          prop: "sfCheckword",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "settlementCode",
          prop: "settlementCode",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "中通快运 cpcode",
          prop: "ztocpcode",
          input: "input",
          desc: "暂未启用",
        },
        {
          label: "中通快运 商家密钥",
          prop: "ztosign",
          input: "input"
        },
        {
          label: "中通快运 来源id ecid",
          prop: "ztoecid",
          input: "input"
        },
        {
          label: this.$t("app.addName"),
          prop: "insUser",
        },
        {
          label: this.$t("app.addDate"),
          prop: "insTime",
        },
        {
          label: this.$t("app.updateName"),
          prop: "updUser",
        },
        {
          label: this.$t("app.updateDate"),
          prop: "updTime",
        },
      ];
    },
    formConfig() {
      return [
        {
          label: "配置名称",
          prop: "configname",
          type: "input",
        },
        {
          label: "生效仓库",
          prop: "wmwhseid",
          type: "input",
        },
        {
          label: "出库单货主编码",
          prop: "storerkey",
          type: "input",
        },
        {
          label: "出库单承运商编码",
          prop: "carriercode",
          type: "input",
        },
        {
          label: "出库单平台编码",
          prop: "rtxsalesp",
          type: "input",
        },
        {
          label: "快递接口",
          prop: "expressapi",
          type: 'select',
          options: this.options.expressapi
        },
        {
          label: "打印模板url",
          prop: "templatesurl",
          type: "input",
          desc: "货主平台 为淘系 拼多多 抖音需填",
        },
        {
          label: "平台承运商编码",
          prop: "platformCarriercode",
          type: "input",
          desc: "",
        },
        {
          label: "刷新token",
          prop: "refreshtoken",
          type: "input",
        },
        {
          label: "发货人名称",
          prop: "sendName",
          type: "input",
        },
        {
          label: "发货公司",
          prop: "sendCompanyName",
          type: "input",
        },
        {
          label: "发货人手机",
          prop: "sendMobile",
          type: "input",
        },
        {
          label: "发货人电话",
          prop: "sendPhone",
          type: "input",
        },
        {
          label: "发货人省份",
          prop: "sendProvince",
          type: "input",
        },
        {
          label: "发货人城市",
          prop: "sendCity",
          type: "input",
        },
        {
          label: "发货人区县",
          prop: "sendCounty",
          type: "input",
        },
        {
          label: "发货人乡镇街道",
          prop: "sendTown",
          type: "input",
        },
        {
          label: "发货人详细地址",
          prop: "sendAddress",
          type: "input",
        },
        {
          label: "接口请求url",
          prop: "url",
          type: "input",
        },
        {
          label: "appkey",
          prop: "appkey",
          type: "input",
          desc: "暂未启用",
        },
        {
          label: "appSecret",
          prop: "appSecret",
          type: "input",
          desc: "暂未启用",
        },
        {
          label: "clientId 拼多多",
          prop: "clientId",
          type: "input",
          desc: "暂未启用",
        },
        {
          label: "clientSecret 拼多多",
          prop: "clientSecret",
          type: "input",
          desc: "暂未启用",
        },
        {
          label: "sign 德邦",
          prop: "sign",
          type: "input",
          desc: "暂未启用",
        },
        {
          label: "companycode 德邦",
          prop: "companycode",
          type: "input",
          desc: "暂未启用",
        },
        {
          label: "ztoCompanyid 中通",
          prop: "ztoCompanyid",
          type: "input",
          desc: "暂未启用",
        },
        {
          label: "ztoKey 中通",
          prop: "ztoKey",
          type: "input",
          desc: "暂未启用",
        },
        {
          label: "ztoPartner 中通",
          prop: "ztoPartner",
          type: "input",
          desc: "可配置",
        },
        {
          label: "stoSecretkey 申通",
          prop: "stoSecretkey",
          type: "input",
          desc: "暂未启用",
        },
        {
          label: "stoSiteCode 申通",
          prop: "stoSiteCode",
          type: "input",
          desc: "暂未启用",
        },
        {
          label: "stoCustomerName 申通",
          prop: "stoCustomerName",
          type: "input",
          desc: "暂未启用",
        },
        {
          label: "stoCode 申通",
          prop: "stoCode",
          type: "input",
          desc: "暂未启用",
        },
        {
          label: "sitePwd 申通",
          prop: "sitePwd",
          type: "input",
          desc: "暂未启用",
        },
        {
          label: "sfCustid 顺丰",
          prop: "sfCustid",
          type: "input",
          desc: "暂未启用",
        },
        {
          label: "sfCheckword 顺丰",
          prop: "sfCheckword",
          type: "input",
          desc: "暂未启用",
        },
        {
          label: "settlementCode",
          prop: "settlementCode",
          type: "input",
          desc: "暂未启用",
        }, 
        {
          label: "中通快运 CPcode",
          prop: "ztocpcode",
          type: "input",
          desc: "示例 5710******016",
        },
        {
          label: "中通快运 商家密钥",
          prop: "ztosign",
          type: "input",
          desc: "示例 ztky_468b076f-*****-acfa-13592ff0d702",
        },        {
          label: "中通快运 来源ecid",
          prop: "ztoecid",
          type: "input",
          desc: "示例 ztky_468b076f-*****-acfa-13592ff0d702",
        },
      ];
    },
    formRules() {
      return {
        applicationId: [
          {
            required: true,
            message: "请选择源应用APP",
            trigger: "change",
          },
        ],
        apiRequestRootType: [
          {
            required: true,
            message: "请选择报文根节点数据类型",
            trigger: "change",
          },
        ],
      };
    },
    apiMap() {
      return {
        getListApi: "/v1/PlExpressApiConfig/selectExpressApiConfigPageList",
        getInfoApi: "/v1/PlExpressApiConfig/selectExpressApiConfigById",
        deleteApi: "/v1/PlExpressApiConfig/deleteExpressApiConfigIds",
        editApi: "/v1/PlExpressApiConfig/editExpressApiConfig",
        addApi: "/v1/PlExpressApiConfig/addExpressApiConfig",
      };
    },
  },
  methods: {
    handleConfirm() {
      this.$refs.detail.validate().then((valid) => {
        const params = this._.cloneDeep(this.form);
        const appObj = this.options.app.find(
          (opt) => opt.id == params.applicationId
        );
        let promise;
        if (this.dialogModule.type == "edit") {
          promise = this.$post(this.apiMap.editApi, params);
        } else {
          promise = this.$post(this.apiMap.addApi, params);
        }
        promise.then((res) => {
          this.$message.success(res.returnMgr);
          this.getTableData();
          this.$refs.detail?.resetFields();
          this.dialogModule.flag = false;
        });
      });
    },
    getInfoById(id) {
      this.$post(this.apiMap.getInfoApi, {
        id,
      }).then((res) => {
        this.form = res.data.item;
        this.dialogModule.flag = true;
      });
    },
    setdialog(data) {
      this.dialogModule.title = `编辑[${data.apiName}]快递配置信息`;
      this.dialogModule.type = "edit";
      this.dialogModule.data = data;
      this.getInfoById(data.id);
    },
    newCreate() {
      this.dialogModule.title = "新增快递配置信息";
      this.dialogModule.type = "add";
      this.dialogModule.data = null;
      this.dialogModule.flag = true;
    },
    //删除
    async del(data) {
      this.$post(this.apiMap.deleteApi, data).then((res) => {
        this.$message.success(res.returnMgr);
        this.getTableData();
      });
    },
    getOptions() {
          this.$post('/v1/PlExpressApiConfig/selectExpressApiSelect', {}).then(res => {
          this.options.expressapi = res.data.item;
        })

    },
    closed() {
      this.$refs.detail?.resetFields();
    },
    cbFun(res) {
      return { list: res.data.page.records, total: res.data.page.total };
    },
    async getTableData() {
      this.$refs.table.getTableData();
    },
  },
  mounted() {
    this.getOptions();
  },
  components: {
    "v-tableTitle": tableTitle,
  },
};
</script>

<style scoped></style>
